const { sequelize } = require("@/db");
const { formatResponse } = require("@/utils/responseFormatter.js");

// 根据id获取当前用户信息
exports.getCurrentUserById = async (ctx, next) => {
  const { id } = ctx.params;
  const sql = `select * from av_users where id = ?`;
  try {
    const results = await sequelize.query(sql, {
      replacements: [id],
      type: sequelize.QueryTypes.SELECT,
    });

    // 检查查询结果是否正确
    if (results === undefined || results.length !== 1) {
      formatResponse(ctx, 400, "获取用户失败,未找到指定用户或找到多个用户");
    }
    // 返回查询到的用户信息
    else {
      formatResponse(ctx, 200, "获取用户成功", 20000, results);
    }
  } catch (err) {
    console.error("获取用户失败: ", err);
    formatResponse(ctx, 500, "数据库操作失败");
  }
};
